package com.wanghlme.day22.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class Price2Hans extends HttpServlet {
    private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] I_UNIT = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};
    private static final String[] D_UNIT = {"角", "分", "厘"};

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        shop(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public void shop(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        JSONObject jo = JSONObject.parseObject(request.getParameter("data2"));
        String str1 = jo.getString("str1");
        String str2 = jo.getString("str2");
        String str3 = jo.getString("str3");

        str2 = ConvertUpMoney.toChinese(str2);

        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert out != null;
        String tmp = "{\"str1\":\"" + str1 + "\", \"str2\":\"" + str2 + "\", \"str3\":\"" + str3 + "\"}";
        out.write(tmp);
        out.flush();
        out.close();
    }
}

class ConvertUpMoney {

    private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] I_UNIT = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};
    private static final String[] D_UNIT = {"角", "分", "厘"};

    private static final String REGEX_1;

    static {
        REGEX_1 = "(-)?[\\d]*(.)?[\\d]*";
    }

    public static String toChinese(String str) {
        if (StringUtils.isBlank(str) || !str.matches(REGEX_1)) {
            return "抱歉，请输入数字！";
        }

        if ("0".equals(str) || "0.00".equals(str) || "0.0".equals(str)) {
            return "零元";
        }

        boolean flag = false;
        if (str.startsWith("-")) {
            flag = true;
            str = str.replaceAll("-", "");
        }

        str = str.replaceAll(",", "");
        String integerStr;
        String decimalStr;

        if (str.indexOf(".") > 0) {
            integerStr = str.substring(0, str.indexOf("."));
            decimalStr = str.substring(str.indexOf(".") + 1);
        } else if (str.indexOf(".") == 0) {
            integerStr = "";
            decimalStr = str.substring(1);
        } else {
            integerStr = str;
            decimalStr = "";
        }

        if (integerStr.length() > I_UNIT.length) {
            return "超出计算能力！";
        }


        int[] integers = toIntArray(integerStr);
        if (integers.length > 1 && integers[0] == 0) {
            return "抱歉，输入数字不符合要求！";
        }

        boolean isWan = isWan5(integerStr);

        int[] decimals = toIntArray(decimalStr);

        String result = getChineseInteger(integers, isWan) + getChineseDecimal(decimals);
        if (flag) {
            return "负" + result;
        } else {
            return result;
        }
    }

    private static int[] toIntArray(String number) {
        int[] array = new int[number.length()];
        for (int i = 0; i < number.length(); i++) {
            array[i] = Integer.parseInt(number.substring(i, i + 1));
        }
        return array;
    }

    public static String getChineseInteger(int[] integers, boolean isWan) {
        StringBuilder chineseInteger = new StringBuilder("");
        int length = integers.length;
        if (length == 1 && integers[0] == 0) {
            return "";
        }
        for (int i = 0; i < length; i++) {
            String key = "";
            if (integers[i] == 0) {
                if ((length - i) == 13) {
                    key = I_UNIT[4];
                } else if ((length - i) == 9) {
                    key = I_UNIT[8];
                } else if ((length - i) == 5 && isWan) {
                    key = I_UNIT[4];
                } else if ((length - i) == 1) {
                    key = I_UNIT[0];
                }
                if ((length - i) > 1 && integers[i + 1] != 0) {
                    key += NUMBERS[0];
                }
            }
            chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + I_UNIT[length - i - 1]));
        }
        return chineseInteger.toString();
    }

    private static String getChineseDecimal(int[] decimals) {
        StringBuilder chineseDecimal = new StringBuilder("");
        for (int i = 0; i < decimals.length; i++) {
            if (i == 3) {
                break;
            }
            chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + D_UNIT[i]));
        }
        return chineseDecimal.toString();
    }

    private static boolean isWan5(String integerStr) {
        int length = integerStr.length();
        if (length > 4) {
            String subInteger = "";
            if (length > 8) {
                subInteger = integerStr.substring(length - 8, length - 4);
            } else {
                subInteger = integerStr.substring(0, length - 4);
            }
            return Integer.parseInt(subInteger) > 0;
        } else {
            return false;
        }
    }
}